package com.java110.mall.acct.payment.business;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.payment.PaymentOrderDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shop.ShopConfigDto;
import com.java110.dto.storeOrder.StoreOrderReturnPayDto;
import com.java110.intf.cart.IUnifiedOrderInnerServiceSMO;
import com.java110.intf.shop.IShopConfigV1InnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.mall.acct.payment.IPaymentBusiness;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("buyGoods")
public class BuyGoodsBusiness implements IPaymentBusiness {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IUnifiedOrderInnerServiceSMO unifiedOrderInnerServiceSMOImpl;

    @Autowired
    private IShopConfigV1InnerServiceSMO shopConfigV1InnerServiceSMOImpl;

    @Override
    public PaymentOrderDto unified(ICmdDataFlowContext context, JSONObject reqJson) {

        String appId = context.getReqHeaders().get("app-id");
        String userId = context.getReqHeaders().get("user-id");
        reqJson.put("user-id", userId);//
        reqJson.put("appId", appId);//
        //todo 查询商铺信息
        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(reqJson.getString("shopId"));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");

        //检查是否在营业时间内
        validateShopOpen(shopDtos.get(0));

        shopDto = shopDtos.get(0);

        StoreOrderReturnPayDto storeOrderReturnPayDto = unifiedOrderInnerServiceSMOImpl.unifiedOrder(reqJson);
        String orderId = storeOrderReturnPayDto.getOrderId();
        String feeName = "购买商品";
        double money = storeOrderReturnPayDto.getPayPrice();

        PaymentOrderDto paymentOrderDto = new PaymentOrderDto();
        paymentOrderDto.setOrderId(orderId);
        paymentOrderDto.setMoney(money);
        paymentOrderDto.setName(feeName);
        if (StringUtil.isNumber(shopDto.getServiceFee()) && Double.parseDouble(shopDto.getServiceFee()) > 0) {
            paymentOrderDto.setProfitSharing(true);
        }

        return paymentOrderDto;
    }

    private void validateShopOpen(ShopDto shopDto) {

        ShopConfigDto shopConfigDto = new ShopConfigDto();
        shopConfigDto.setShopId(shopDto.getShopId());
        List<ShopConfigDto> shopConfigDtos = shopConfigV1InnerServiceSMOImpl.queryShopConfigs(shopConfigDto);
        if (ListUtil.isNull(shopConfigDtos)) {
            return;
        }

        Calendar today = Calendar.getInstance();
        String todayStr = DateUtil.getFormatTimeStringB(today.getTime());

        Date startTime = null;
        Date endTime = null;
        startTime = DateUtil.getDateFromStringA(todayStr + " " + shopConfigDtos.get(0).getOstartTime() + ":00");
        endTime = DateUtil.getDateFromStringA(todayStr + " " + shopConfigDtos.get(0).getOendTime() + ":00");

        if (endTime.getTime() < startTime.getTime()) {
            Calendar endTimeCal = Calendar.getInstance();
            endTimeCal.setTime(endTime);
            endTimeCal.add(Calendar.DAY_OF_MONTH, 1);
            endTime = endTimeCal.getTime();
        }
        if (!DateUtil.belongCalendar(today.getTime(), startTime, endTime)) {
            throw new CmdException("店铺休息");
        }
    }

    @Override
    public void notifyPayment(PaymentOrderDto paymentOrderDto, JSONObject reqJson) {

        reqJson.put("orderId", paymentOrderDto.getOrderId());
        reqJson.put("payOrderId", paymentOrderDto.getTransactionId());
        reqJson.put("sendIntegral", paymentOrderDto.getSendIntegral());
        reqJson.put("sendIntegralMoney", paymentOrderDto.getSendIntegralMoney());
        ResponseEntity<String> responseEntity = unifiedOrderInnerServiceSMOImpl.payNotifyOrder(reqJson);

    }
}
